<?php

namespace app\common\model\mysql;

use think\Model;

class SDistribute extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 查询分发平台数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalDistributeList($where, $field,$limit,$page){
        $order = [
            "distribute_id" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    public function getDistributeByNameModel($distribute_name,$distribute_id){
        if(empty($distribute_name)){
            return false;
        }
        $where = [
            "distribute_name" =>$distribute_name,
            "del_flag"=>'0',
        ];
        if($distribute_id){
            $result = $this->where("distribute_id", "<>", $distribute_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 根据id获取分发平台信息
     * @param $id
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDistributeIdData($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "distribute_id" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function delDistributeIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['distribute_id', 'in', $ids],
            ['del_flag', '=', '0']
        ];
        $data = [
            'del_flag'=>'2'
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }
}
